# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer: Leo <thinkabit.ukim@gmail.com>
pkgname=elogind
pkgver=243.7
pkgrel=1
pkgdesc="Standalone fork of systemd's elogind"
url="https://github.com/elogind/elogind"
arch="all"
license="GPL-2.0-or-later LGPL-2.1-or-later"
depends="dbus shadow"
options="!check" # Tests fail on builders
makedepends="
	meson
	coreutils
	docbook-xsl
	gettext-dev
	git
	gperf
	intltool
	libxslt-dev
	glib-dev
	acl-dev
	eudev-dev
	libcap-dev
	libseccomp-dev
	linux-pam-dev
	m4
	dbus-dev
	pcre2-dev
	"
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-lang
	$pkgname-openrc
	lib$pkgname:libs
	$pkgname-zsh-completion:zshcomp:noarch
	$pkgname-bash-completion:bashcomp:noarch
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/elogind/elogind/archive/v$pkgver.tar.gz
	elogind.initd
	"

build() {
	[ "$CARCH" == "s390x" ] && export CFLAGS="$CFLAGS -D__IGNORE_pkey_mprotect -DSO_PEERSEC=31"
	export LDFLAGS="$LDFLAGS -lintl"
	meson \
		--libdir=/usr/lib \
		--libexecdir=/usr/libexec \
		--buildtype=plain \
		-Dprefix=/usr \
		-Dcgroup-controller=elogind \
		-Dhalt-path=/sbin/halt \
		-Drootlibexecdir=/usr/libexec/elogind \
		-Dreboot-path=/sbin/reboot \
		-Ddefault-hierarchy=hybrid \
		-Ddefault-kill-user-processes=false \
		-Dpolkit=true \
		build
	ninja -C build
}

check() {
	ninja -C build test
}

package() {
	DESTDIR="$pkgdir" ninja -C build install

	# Claim compatiblity with systemd and systemd-logind
	ln -s libelogind.pc "$pkgdir"/usr/lib/pkgconfig/libsystemd.pc
	ln -s libelogind.pc "$pkgdir"/usr/lib/pkgconfig/libsystemd-logind.pc
	ln -s "$pkgdir"/usr/include/elogind "$pkgdir"/usr/include/systemd

	# Install headers from elogind
	install -Dm644 src/systemd/sd-id128.h usr/include/sd-id128.h
	install -Dm644 src/systemd/_sd-common.h usr/include/_sd-common.h

	# Install openrc service
	install -Dm755 "$srcdir"/elogind.initd "$pkgdir"/etc/init.d/elogind
}

zshcomp() {
	pkgdesc="Zsh completion for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel zsh"

	mkdir -p "$subpkgdir"/usr/share
	mv "$pkgdir"/usr/share/zsh "$subpkgdir"/usr/share/
}

bashcomp() {
	pkgdesc="Bash completion for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	mkdir -p "$subpkgdir"/usr/share
	mv "$pkgdir"/usr/share/bash-completion "$subpkgdir"/usr/share
}

sha512sums="fd17c5016d083d63805f9ed0326ee32597870f6c48c9c246712cb09a77db775036b0fd0f4258b6557e189a1eceb4b50f4ae2e9e7881f4d9759b87a0b49ce3472  elogind-243.7.tar.gz
e73738488e78af099fdbb7c93a411a3e8d69080a93f89c245cb8c6de5fe4cb7fe724687d19e21eb6be52ccc795ec200d9c6e499efb1afd2b27467f7f541a7dd1  elogind.initd"
